﻿DDWin=function(options){
	window.curDDWin=this;
	this.init=function(){
		if(document.getElementById("DDwindows"))return;
		var actualWidth;
		if($(document.getElementById(options.dep)).width() < 210){
			actualWidth = 210;
		}else{
			actualWidth = $(document.getElementById(options.dep)).width();
		}
		var w=options.width||actualWidth;
		var h=options.height||300;
		$("body").append("<div id='DDwindows' style='width:"+w+"px;height:"+h+"px;position:absolute;z-index:100;border:#7f9db9 1px solid;background:#FFFFFF'></div>");
		this.elParent = $("#"+options.el).parent();
		this.cloneEl = $("#"+options.el).clone(true);
		this.clickType = options.clickBody;
		this.el=options.el;
		this.resize();

		this.minWidth=options.minWidth||300;
		this.minHeight=options.minHeight||200;
		
		//内容
		$("#"+options.el).appendTo($("#DDwindows"));
		$("#"+options.el).css("display","block");
		$("#"+options.el).css("height",h+"px");
		$("#"+options.el).css("width",w+"px");
		$($("#"+options.el).children().get(0)).css("width",w+"px");
		//允许缩放
		$(document.getElementById(options.dep)).bind('resize',this.resize);
		//$("#"+options.dep).bind('resize',this.resize);
		$("body").bind("mousedown",
			function(event){
			 if (!(event.target.id == "DDwindows" || $(event.target).parents("#DDwindows").length>0)) {
				 try{
					 if(window.curDDWin.clickType == "save"){
						 window.curDDWin.saveWin();
					 }else{
						 window.curDDWin.closeWin();
					 }
					
				 }catch(err){}
			}
		});
	}

	this.resize=function(){
		var dep = $(document.getElementById(options.dep)).offset();
		var _h = $(document.getElementById(options.dep)).height();
	    var _top = dep.top;
	    var _wh = $(window).height();
	    var poph = $("#DDwindows").height();
		var t;
		
	    if ((_wh - (_h + _top)) > poph) {
			t=options.top||dep.top+$(document.getElementById(options.dep)).outerHeight();
	    }else{
	    	t=options.top||(_top-poph);
	    }
		var l=(dep.left);
		if(l<0)l=0;
		if(t<0)t=0;
		
		var cWidth = window.innerWidth || document.documentElement.clientWidth;
		if(cWidth - (l + $("#DDwindows").width()) < 10){
			$("#DDwindows").css({right:0 + "px", top:t + "px"}).slideDown("fast");
		}else{
			$("#DDwindows").css({left:l + "px", top:t + "px"}).slideDown("fast");
		}
		
		//$('#DDwindows').bgiframe();
	}
	
	this.closeWin=function(){
		$(this.cloneEl).appendTo(this.elParent);
		$(document.getElementById(options.dep)).unbind('resize',this.resize);
		$("#"+this.el).hide();
		$("#DDwindows").fadeOut("fast");
		$(this.DDWin).remove();
		$("#DDwindows").remove();
		window.curDDWin=null;
		window[this]=null;
	}
	
	this.saveWin=function(){
		$("#"+this.el).appendTo(this.elParent);
		$(document.getElementById(options.dep)).unbind('resize',this.resize);
		$("#"+this.el).hide();
		$("#DDwindows").fadeOut("fast");
		$(this.DDWin).remove();
		$("#DDwindows").remove();
		window.curDDWin=null;
		window[this]=null;
	}

	this.options=options;
	this.init();

}	
